<?php

namespace common\models;

use Yii;
use himiklab\sitemap\behaviors\SitemapBehavior;
use yii\helpers\Url;

/**
 * This is the model class for table "note".
 *
 * @property integer $id
 * @property integer $cid
 * @property string $title
 * @property string $author
 * @property string $description
 * @property string $content
 * @property string $tag
 * @property integer $view
 * @property integer $enabled
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property Catelog $c
 */
class Note extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'note';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cid', 'view', 'enabled', 'created_at', 'updated_at'], 'integer'],
            [['content'], 'string'],
            [['title', 'tag'], 'string', 'max' => 100],
            [['author'], 'string', 'max' => 20], 
            [['description'], 'string', 'max' => 255],
            [['title'], 'unique'],
            [['cid'], 'exist', 'skipOnError' => true, 'targetClass' => Catelog::className(), 'targetAttribute' => ['cid' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'cid' => Yii::t('common', '目录ID'),
            'title' => Yii::t('common', '标题'),
            'author' => Yii::t('common', '作者'),
            'description' => Yii::t('common', '描述'),
            'content' => Yii::t('common', '内容'),
            'tag' => Yii::t('common', '标签'),
            'view' => Yii::t('common', '浏览量'),
            'enabled' => Yii::t('common', '是否显示'),
            'created_at' => Yii::t('common', '创建时间'),
            'updated_at' => Yii::t('common', '更新时间'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getC()
    {
        return $this->hasOne(Catelog::className(), ['id' => 'cid'])->inverseOf('notes');
    }

    /**
     * 设置自动更新时间   created_at | updated_at 字段类型为 int(11) 
     */
    public function behaviors() {
        return [
            \yii\behaviors\TimestampBehavior::className(),
        ];
    }    
}
